package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.dto.ApAuthor;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.MediaFeign;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;

import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.krb5.internal.APOptions;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-06-20
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {
    @Autowired
    IApUserService userService;

    @Autowired
    ArticleFeign articleFeign;
    @Autowired
    MediaFeign mediaFeign;





    /**
     * 根据状态分页查询用户注册详细信息
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult listByStatus(AuthDto authDto) {


        LambdaQueryWrapper<ApUserRealname> query=new LambdaQueryWrapper<>();
        if(authDto.getStatus()!=null){
            query.eq(ApUserRealname::getStatus,authDto.getStatus());
        }
        IPage<ApUserRealname> page=new Page<>(authDto.getPage(),authDto.getSize());
        IPage<ApUserRealname> realnameIPage = this.page(page, query);
        PageResponseResult result=new PageResponseResult(authDto.getPage(),authDto.getSize(),realnameIPage.getTotal(),realnameIPage.getRecords());
        return result;
    }

    @Override
    @GlobalTransactional
    public ResponseResult auth(AuthDto dto,Integer type) {
        ApUserRealname apUserRealname = this.getById(dto.getId());
        //审核不通过
        if(type==0){
            apUserRealname.setStatus(2);
            apUserRealname.setReason(dto.getReason());
            this.updateById(apUserRealname);
            return ResponseResult.okResult();
        }
        //审核通过
        //根据用户注册表创建自媒体账号
        ApUser apUser = userService.getById(apUserRealname.getUserId());
         WmUser wmUser=createWmUser(apUser);
         if(wmUser != null){
             //创建文章作者账号
            ApAuthor apAuthor= createApAuthor(apUser,wmUser);
            if(apAuthor!=null){
                //修改用户认证状态
                apUserRealname.setStatus(9);
                this.updateById(apUserRealname);
                //修改用户注册表的flag为1 自媒体人
                apUser.setFlag(1);
                userService.updateById(apUser);
            }
         }
        return ResponseResult.okResult();
    }

    /**
     * 创建文章作者账号
     * @param apUser
     * @param wmUser
     * @return
     */
    private ApAuthor createApAuthor(ApUser apUser, WmUser wmUser) {
        ApAuthor apAuthor=new ApAuthor();
        apAuthor.setName(apUser.getName());
        apAuthor.setType(2);
        apAuthor.setUserId(apUser.getId());
        apAuthor.setWmUserId(wmUser.getId());
        ResponseResult<ApAuthor> result = articleFeign.saveApAuthor(apAuthor);
        if(result.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())){
            ApAuthor author = result.getData();
            if(apAuthor!=null){
                wmUser.setApUserId(author.getId());
                //更新自媒体账户
                mediaFeign.updateWmUser(wmUser);
            }
            return author;
        }
        return  null;

    }





    /**
     * 保存自媒体账号
     * @param apUser
     * @return
     */
    private WmUser createWmUser(ApUser apUser) {
        WmUser wmUser=new WmUser();
        BeanUtils.copyProperties(apUser,wmUser);
        wmUser.setId(null);
        wmUser.setApUserId(apUser.getId());
        wmUser.setNickname(apUser.getName());
        wmUser.setStatus(9);
        wmUser.setType(0);
        //发起远程调用
        ResponseResult<WmUser> result = mediaFeign.saveWmUser(wmUser);
        if(result.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())){
            WmUser user = result.getData();
            return user;
        }
         return null;
    }
}
